package com.example.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edit_id;
    private EditText edit_name;
    private EditText edit_score;
    private Button button_add;
    private Button button_select;
    private Button button_del;
    private Button button_update;
    private EditText edit_content;
    DBHelper helper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        helper=new DBHelper(this);
    }

    private void initView() {
        edit_id = (EditText) findViewById(R.id.edit_id);
        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_score = (EditText) findViewById(R.id.edit_score);
        button_add = (Button) findViewById(R.id.button_add);
        button_select = (Button) findViewById(R.id.button_select);
        button_del = (Button) findViewById(R.id.button_del);
        button_update = (Button) findViewById(R.id.button_update);
        edit_content = (EditText) findViewById(R.id.edit_content);

        button_add.setOnClickListener(this);
        button_select.setOnClickListener(this);
        button_del.setOnClickListener(this);
        button_update.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String id = edit_id.getText().toString().trim();
        String name = edit_name.getText().toString().trim();
        String score = edit_score.getText().toString().trim();
        ContentValues values=new ContentValues();
        edit_content.setText("");
        int number=0;
        switch (v.getId()) {
            case R.id.button_add:
                if (TextUtils.isEmpty(id)) {
                    Toast.makeText(this, "id不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(score)) {
                    Toast.makeText(this, "score不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                db=helper.getWritableDatabase();
                values.put(DBUtil.STU_ID,id);
                values.put(DBUtil.STU_NAME,name);
                values.put(DBUtil.STU_SCORE,score);
                db.insert(DBUtil.TABLE_NAME,null,values);
                edit_content.setText("已成功添加：\n"+id+","+name+","+score);
                break;
            case R.id.button_select:
                db = helper.getReadableDatabase();
                Cursor cursor=null;
                if (TextUtils.isEmpty(id)) {
                    cursor = db.query(DBUtil.TABLE_NAME, null, null, null,null, null, null);
                }
                else {
                    cursor = db.query(DBUtil.TABLE_NAME, null, DBUtil.STU_ID+"=?", new String[]{id+""},null, null, null);
                }
                String content="查询结果为空";
                if (cursor.getCount() != 0){
                    content="查询结果如下：\n";
                    while (cursor.moveToNext()){
                        String _id1 = cursor.getString(cursor.getColumnIndex(DBUtil.STU_ID));

                        String name1 = cursor.getString(cursor.getColumnIndex(DBUtil.STU_NAME));
                        String score1 = cursor.getString(cursor.getColumnIndex(DBUtil.STU_SCORE));
                        content= content+_id1+","+name1+","+score1+"\n";
                    }
                }
                else {
                    Toast.makeText(this, "查询结果为空", Toast.LENGTH_SHORT).show();
                }
                edit_content.setText(content);

                break;
            case R.id.button_del:
                if (TextUtils.isEmpty(id)) {
                    Toast.makeText(this, "id不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                db = helper.getWritableDatabase();
                number = db.delete(DBUtil.TABLE_NAME, DBUtil.STU_ID+"=?", new String[]{id+""});
                edit_content.setText("删除成功");
                break;
            case R.id.button_update:
                String str="请先输入修改内容";
                String []ids=id.split(",");
                String whereClause="";
                for(int i=0;i<ids.length-1;i++){
                    whereClause=whereClause+DBUtil.STU_ID+"=?"+" or ";
                }
                whereClause=whereClause+DBUtil.STU_ID+"=?";
                System.out.println(ids.length);

                if (TextUtils.isEmpty(id)) {
                    Toast.makeText(this, "id不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                db = helper.getWritableDatabase();
                if (!TextUtils.isEmpty(name)) {
                    values.put(DBUtil.STU_NAME, name);
                    number = db.update(DBUtil.TABLE_NAME, values, whereClause,ids);
                    str="姓名修改成功\n";
                }
                if (!TextUtils.isEmpty(score)) {
                    values.put(DBUtil.STU_SCORE, score);
                    number = db.update(DBUtil.TABLE_NAME, values, whereClause,ids);
                    if(str.equals("请先输入修改内容")){
                        str="分数修改成功\n";
                    }
                    else {
                        str=str+"分数修改成功\n";
                    }

                }
                edit_content.setText(str);
                break;
        }
        edit_id.setText("");
        edit_name.setText("");
        edit_score.setText("");
        db.close();
    }

    private void submit() {
        // validate
        String id = edit_id.getText().toString().trim();
        String name = edit_name.getText().toString().trim();
        String score = edit_score.getText().toString().trim();
        String content = edit_content.getText().toString().trim();
         /*
        if (TextUtils.isEmpty(id)) {
            Toast.makeText(this, "id不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(score)) {
            Toast.makeText(this, "score不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
       if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "content不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        *///不可为空






    }
}
